// pages/game/gameOrderList/gameOrderList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderList: [], //查询订单
    pageNum: 1,
    pageSize: 15,
  },
  over(e) {
    let that = this
    var data = e.currentTarget.dataset.data
    var orderId = e.currentTarget.dataset.data

    wx.showModal({
      title: '警告',
      content: '是否确定提前结束和该用户的订单？',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')

          //// 用户同意提前结束订单
          let postObj = {
            url: '/manage/client/order/earlyEnd',
            method: 'PUT',
            data: {
              orderId: orderId,
              token: wx.getStorageSync('token'),
              userId: wx.getStorageSync('userId'),
            }
          }
          app.request.gameRequest(postObj).then(res => {
            console.log('用户同意提前结束订单', res)
            wx.showToast({
              title: '已经提前结束！',
              icon: 'none',
              duration: 2000,
              success: () => {
                setTimeout(() => {
                  that.setData({
                    pageNum: 1,
                    pageSize: 15,
                    orderList: []
                  })
                  that.orderList()
                }, 2000)
              }
            })

          }).catch(err => {
            wx.showToast({
              title: '提前结束失败！',
              icon: 'none',
              duration: 2000,
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })



  },
  toSaySomething(e) {
    let that = this
    var data = e.currentTarget.dataset.data
    var orderId = e.currentTarget.dataset.data
    console.log('orderId', orderId);
    wx.navigateTo({
      url: '/pages/game/trainingDetails/trainingDetails?orderId=' + orderId,
    })
  },
  // 用户查询订单
  orderList() {
    var getObj = {
      url: '/manage/client/order/list/user',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        userId: wx.getStorageSync('userId'),
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize,
      }
    }
    app.request.gameRequest(getObj).then(res => {
      console.log('用户查询订单', res.data.data.list)
      this.setData({
        orderList: [...res.data.data.list, ...this.data.orderList]
      })
    }).catch(err => {
      console.log('我的陪玩明细', err)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNum: 1,
      pageSize: 15,
      orderList: []
    })
    this.orderList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var pageNum = this.data.pageNum + 1

    this.setData({
      pageNum: pageNum
    })
    this.orderList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})